<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="创建时间">
        <el-date-picker
                v-model="dateRange"
                size="small"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="教材" prop="temaId">
          <el-select
            v-model="queryParams.temaId"
            filterable
            clearable=""
            remote
            reserve-keyword
            placeholder="请输入教材名称或教材编号"
            :remote-method="remoteMethodQueryByTema"
            :loading="loading">
            <el-option
              v-for="item in asdeListOptionsQueryOptions"
              :key="item.id"
              :label="item.temaName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >入库</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-minus"
          size="mini"
          @click="handleMins"
        >出库</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
    </el-row>

    <el-table ref="uiTable" v-loading="loading" :data="asdeListOptions" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50"/>
      <el-table-column label="资产编号" align="center" prop="asseCode" :show-overflow-tooltip="true" />
      <el-table-column label="资产名称" align="center" prop="asseName" />
      <el-table-column label="库存时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理前数量" align="center" prop="asdeProcNotCount" />
      <el-table-column label="出/入库类型" align="center" prop="asdeType" >
        <template slot-scope="scope">
          {{
            tmsdSkuTypeOptions.filter(function(item, index, arr){
              return  item.dictValue == scope.row.asdeType
            })[0].dictLabel
          }}
        </template>
      </el-table-column>
      <el-table-column label="当次数量" align="center" prop="asdeCount" >
        <template slot-scope="scope">
          <span v-if="scope.row.asdeType !=1">- </span>{{scope.row.asdeCount}}
        </template>
      </el-table-column>
      <el-table-column label="当前库存数量" align="center" prop="asdeProcAfterCount" />
      <!-- <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button
                  size="mini"
                  circle type="primary" icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  title="修改"
          />
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="资产" prop="asseId">
          <el-select
            v-model="form.asseId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入资名称或资产编号"
            :remote-method="remoteMethodByTema"
            :loading="loading">
            <el-option
              v-for="item in asseListOptions"
              :key="item.id"
              :label="item.asseName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出/入库类型" prop="asdeType" >
            <el-select v-model="form.asdeType" clearable placeholder="请选择" disabled>
              <el-option
                v-for="item in tmsdSkuTypeOptions"
                :disabled="item.dictValue == 0"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="出/入数量" prop="asdeCount">
          <el-input type="input" v-model="form.asdeCount" placeholder="请输入出/入数量"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  storeDetilsList, storeDetilsInfo, storeDetilsAdd, storeDetilsUpdate, storeDetilsDelete, 
  teachingMaterialList } from "@/api/logistics/assetsStoreDetils";

export default {
  name: "termTime",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      indexs:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 资产出入库表格数据
      asdeListOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据字典: 状态
      statusOptions: [],
      // 数据字典: 资产Sku类型
      tmsdSkuTypeOptions: [],
      // 编辑表单：资产：下拉框选项数据
      asseListOptions: [],
      // 查询表单：资产：下拉框选项数据
      asdeListOptionsQueryOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        type: undefined,
        status: undefined,
        temaId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        temaId: [
          { required: true, message: "资产不能为空", trigger: "blur" }
        ],
        tmsdType: [
          { required: true, message: "库存调剂类型不能为空", trigger: "blur" }
        ],
        tmsdCount: [
          { required: true, message: "库存调剂数量不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("tesd_sku_type").then(response => {
      this.tmsdSkuTypeOptions = response.data;
      console.log("this.tmsdSkuTypeOptions", this.tmsdSkuTypeOptions);
    });
    this.remoteMethodQueryByTema("");
    this.remoteMethodByTema("");
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      storeDetilsList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.asdeListOptions = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },

    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        type: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.asdeType="1";
      this.title = "资产入库";
    },
    handleMins() {
      this.reset();
      this.open = true;
      this.form.asdeType="-1";
      this.title = "资产出库";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length

      this.indexs = [];
      selection.forEach((val, index) => {
        this.asdeListOptions.forEach((v, i) => {
          // id 是每一行的数据id
          if(val.id == v.id){
            console.log(i+1);
            this.indexs.push(i+1);
          }
        })
      }) ;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      storeDetilsInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资产";
      });
    },
    handleSocre(row) {
      this.$router.push({path:"/teacher/evaluation/mytask/taskScoreData", query:{teutId: row.teutId}})
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            storeDetilsUpdate(this.form.id, this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.message);
              }
            });
          } else {
            storeDetilsAdd(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.message);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除序号为"' + this.indexs + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return storeDetilsDelete(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportType(queryParams);
      }).then(response => {
        this.fileDownload(response.data, "学年学期.xlsx");
      }).catch(function() {});
    },
    /** 格式化日期 */
    dateFormateBegin:function (date) {
      if (date == undefined) {
        return ""
      } else {
        return moment(date).format("YYYY-MM-DD 00:00:00");
      }
    },
     /** 格式化日期 */
     dateFormateEnd: function(date) {
      if (date == undefined) {
        return ""
      } else {
        return moment(date).format("YYYY-MM-DD 23:59:59");
      }
    },
    // 编辑from远程搜索教材
    remoteMethodByTema(query) {
      this.loading = true;
      teachingMaterialList(query).then( response => {
        this.loading = false;
        this.asseListOptions =  response.data;
      });
    },
    // 查询条件
    remoteMethodQueryByTema(query) {
      this.loading = true;
      teachingMaterialList(query).then( response => {
        this.loading = false;
        this.asdeListOptionsQueryOptions =  response.data;
      });
    }

  }
};
</script>
